package com.ruoyi.business.service.impl;

import com.ruoyi.common.core.domain.TreeSelectString;
import com.ruoyi.common.core.domain.entity.CustomerClass;
import com.ruoyi.business.dao.CustomerClassDao;
import com.ruoyi.business.service.ICustomerClassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * CustomerService业务层处理
 *
 * @author liutietou
 * @since 1.0 2025-06-27
 */
@Transactional(readOnly = true)
@Service
public class CustomerClassServiceImpl implements ICustomerClassService {
    @Autowired
    private CustomerClassDao customerClassDao;


    private Specification<CustomerClass> formatQueryParams(CustomerClass req) {
        Specification<CustomerClass> example = new Specification<CustomerClass>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<CustomerClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotBlank(req.getCcccode())) {
                    Predicate pre = cb.equal(root.get("ccccode").as(String.class), req.getCcccode());
                    list.add(pre);
                }
                if (StringUtils.isNotBlank(req.getCccname())) {
                    Predicate pre = cb.like(root.get("cccname").as(String.class), "%" + req.getCccname() + "%");
                    list.add(pre);
                }

                if (list.isEmpty()) {
                    return null;
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        return example;
    }


    public List<CustomerClass> selectCustomerClassList(CustomerClass req) {
        Specification<CustomerClass> example = formatQueryParams(req);
        List<CustomerClass> list = customerClassDao.findAll(example, Sort.by(Sort.Direction.DESC, "ccccode"));
        return list;
    }


    private List<CustomerClass> getChildList(List<CustomerClass> list, CustomerClass t) {
        List<CustomerClass> tlist = new ArrayList<>();
        Iterator<CustomerClass> it = list.iterator();
        while (it.hasNext()) {
            CustomerClass n = it.next();
            if (com.ruoyi.common.utils.StringUtils.isNotNull(n.getParentcode()) && n.getParentcode().equals(t.getParentcode())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CustomerClass> list, CustomerClass t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }


    public List<TreeSelectString> buildCustomerClassTree(List<CustomerClass> list) {
        List<CustomerClass> trees = buildTree(list);
        return trees.stream().map(TreeSelectString::new).collect(Collectors.toList());
    }


    public List<CustomerClass> buildTree(List<CustomerClass> list) {
        List<CustomerClass> returnList = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        for (CustomerClass customerClass : list) {
            tempList.add(customerClass.getParentcode());
        }
        for (Iterator<CustomerClass> iterator = list.iterator(); iterator.hasNext(); ) {
            CustomerClass dept = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentcode())) {
                recursionFn(list, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = list;
        }
        return returnList;
    }


    private void recursionFn(List<CustomerClass> list, CustomerClass t) {
        // 得到子节点列表
        List<CustomerClass> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CustomerClass tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

}
